package com.myown.exception;

import com.myown.exception.enums.ExpectionEnum;

/**
 * 通用异常类，异常父类，继承自{@link RuntimeException} <p>
 *
 * @author lincky
 * @version v1.0.0
 */
public class CommonException extends RuntimeException {
    private ExpectionEnum expectionEnum;
    private String extralMessage;

    public String getExtralMessage() {
        return extralMessage;
    }

    public ExpectionEnum getExpectionEnum() {
        return expectionEnum;
    }

    public int getCode() {
        return expectionEnum.getCode();
    }

    @Override
    public String getMessage() {
        return expectionEnum.getMessage() +
                (extralMessage == null ? "" : " -details: " + extralMessage);
    }

    public CommonException(ExpectionEnum expectionEnum) {
        super(expectionEnum.getMessage());
        this.expectionEnum = expectionEnum;
    }

    public CommonException(ExpectionEnum expectionEnum, Throwable cause) {
        super(expectionEnum.getMessage(), cause);
        this.expectionEnum = expectionEnum;
    }

    public CommonException(ExpectionEnum expectionEnum, String extralMessage) {
        super(expectionEnum.getMessage() + ": " + extralMessage);
        this.expectionEnum = expectionEnum;
        this.extralMessage = extralMessage;
    }

    public CommonException(ExpectionEnum expectionEnum, String extralMessage, Throwable cause) {
        super(expectionEnum.getMessage() + ": " + extralMessage, cause);
        this.expectionEnum = expectionEnum;
        this.extralMessage = extralMessage;
    }

}
